﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DblogStudio.DAL.Models;

namespace DblogStudio.BLL
{
    public partial class BizNhiemVuBaoTri
    {
        public static List<NhiemVuBaoTri> GetList()
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.NhiemVuBaoTriDbSet.OrderBy(e => e.TenNhiemVuBaoTri).ToList();
            }
        }

        /// <summary>
        /// Lấy theo ID
        /// </summary>
        /// <param name="maCoSoDaoTao"></param>
        /// <returns></returns>
        public static NhiemVuBaoTri GetByID(string maNhiemVu)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.NhiemVuBaoTriDbSet.Where(e => e.MaNhiemVuBaoTri == maNhiemVu).FirstOrDefault();
            }
        }



        /// <summary>
        /// Kiểm tra tên cơ sở đào tạo có trùng hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByName(string tenNhiemVu)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.NhiemVuBaoTriDbSet.Where(e => e.TenNhiemVuBaoTri == tenNhiemVu).FirstOrDefault() != null ? true : false;
            }
        }

        public static CODE_RESULT_RETURN Add(NhiemVuBaoTri obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                if (IsExistByName(obj.TenNhiemVuBaoTri))
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaNhiemVuBaoTri = Guid.NewGuid().ToString();
                try
                {
                    db.NhiemVuBaoTriDbSet.Add(obj);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<NhiemVuBaoTri>(nhatKy, null, obj.MaNhiemVuBaoTri, LOAIDOITUONG.NHIỆM_VỤ_BẢO_TRÌ, LOAITHAOTAC.Thêm);
                    BizNhatKyHoatDong.Add(nhatKy, obj);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.ToString());
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }

        public static CODE_RESULT_RETURN Update(NhiemVuBaoTri obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var may = db.NhiemVuBaoTriDbSet.Where(e => e.MaNhiemVuBaoTri == obj.MaNhiemVuBaoTri).FirstOrDefault();
                if (may != null)
                {
                    var ten = db.NhiemVuBaoTriDbSet.Where(e => e.TenNhiemVuBaoTri == obj.TenNhiemVuBaoTri && e.MaNhiemVuBaoTri != may.MaNhiemVuBaoTri).FirstOrDefault();
                    if (ten != null)
                        return CODE_RESULT_RETURN.TenTrung;
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData(nhatKy, may, may.MaNhiemVuBaoTri, LOAIDOITUONG.NHIỆM_VỤ_BẢO_TRÌ, LOAITHAOTAC.Chỉnh_sửa);
                    may.TenNhiemVuBaoTri = obj.TenNhiemVuBaoTri;
                    may.GhiChu = obj.GhiChu;
                    db.SaveChanges();
                    BizNhatKyHoatDong.Add(nhatKy, may);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                {
                    throw new Exception("Đối tượng này không tồn tại");
                }
            }
        }

        public static CODE_RESULT_RETURN Delete(string maNhiemVu)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var may = db.NhiemVuBaoTriDbSet.Where(e => e.MaNhiemVuBaoTri == maNhiemVu).FirstOrDefault();
                //if (csdt != null)
                //{
                //    if (!(csdt.ListPhongHoc != null && csdt.ListPhongHoc.Count > 0) || (csdt.ListLopKhoaNganh != null && csdt.ListLopKhoaNganh.Count > 0))
                //    {
                db.NhiemVuBaoTriDbSet.Remove(may);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<NhiemVuBaoTri>(nhatKy, may, may.MaNhiemVuBaoTri, LOAIDOITUONG.NHIỆM_VỤ_BẢO_TRÌ, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<NhiemVuBaoTri>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
                //    }
                //    else
                //        return CODE_RESULT_RETURN.ThatBai;
                //}
                //else
                //{
                //    return CODE_RESULT_RETURN.ThatBai;
                //}
            }
        }
    }

}
